<?php

namespace app\admin\controller;

use app\common\business\AdminRole;
use app\common\lib\Curl;
use app\common\lib\Param;
use app\common\lib\Show;
use app\common\lib\Str;
use think\response\Json;

class Department extends AdminBase
{
    /**
     * 管理后台列表
     * @return Json
     */
    public function list(): Json
    {
        list($name, $page,$limit,$status) = Param::getMore([
            ['name', ''],
            ['page', null],
            ['limit', null],
            ['status', null],
        ], $this->request, true);
        try {
            $list=(new \app\common\business\Department())->getTableList($page,$limit,$name,$status);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }

        if (!empty($list['list'])){
            foreach ($list['list'] as $k=>$v){
                $list['list'][$k]['status_text'] = $v['status'] == config('status.mysql.table_normal') ? '正常' : '隐藏';
                $list['list'][$k]['origin_text'] = $v['out_id'] == 0 ? '系统内添加' : '同步就业数据';
            }
        }
        return Show::success($list);
    }

    /**
     * 新增/保存数据
     * @return Json
     */
    public function save(): Json
    {
        list($id, $status,$name,$logo,$introduce,$address) = Param::postMore([
            ['id',null],
            ['status', 1],
            ['name',''],
            ['logo', ''],
            ['introduce', ''],
            ['address', ''],
        ], $this->request, true);

        $data=[
            'id'=>$id,
            'status'=>$status,
            'name'=>$name,
            'logo'=>$logo,
            'introduce'=>$introduce,
            'address'=>$address
        ];
        $validate = new \app\admin\validate\Department();
        $scene=empty($data['id'])?'create':'update';
        if (!$validate->scene($scene)->check($data)) {
            return Show::error([], $validate->getError());
        }
        try {
            $result = (new \app\common\business\Department())->save($data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "保存成功");
        }
        return Show::error([], '保存失败');
    }

    /**
     * 修改状态
     * @return Json
     */
    public function updateStatus(): Json
    {
        list($id, $status) = Param::getMore([
            ['id',null],
            ['status', null],
        ], $this->request, true);

        $data=[
            'id'=>$id,
            'status'=>$status,
        ];
        try {
            $result = (new \app\common\business\Department())->updateStatus($data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "保存成功");
        }
        return Show::error([], '保存失败');
    }

    /**
     * 根据id删除
     * @return Json
     */
    public function deleteById(): Json
    {
        $id=$this->request->get('id');
        try {
            $result=(new \app\common\business\Department())->deleteById($id);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "删除成功");
        }
        return Show::error([], '删除失败');
    }

    /**
     * 更新就业部门数据
     * @return Json
     */
    public function updateListData(): Json
    {
        list($page, $limit) = Param::getMore([
            ['page', 1],
            ['limit', 1000],
        ], $this->request, true);

        $data=[
            'page'=>$page,
            'limit'=>$limit,
        ];

        $departmentBis = new \app\common\business\Department();
        try {
            $list = $departmentBis->getCurlDList($data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }

        try {
            $result = $departmentBis->updateByCurlData($list);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "更新成功");
        }
        return Show::error([], '更新失败');
    }
}
